.TH std::deque::insert 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::deque::insert \- std::deque::insert

.SH Synopsis
   iterator insert( const_iterator pos, const T& value );             \fB(1)\fP
   iterator insert( const_iterator pos, T&& value );                  \fB(2)\fP \fI(since C++11)\fP
   iterator insert( const_iterator pos,                               \fB(3)\fP
                    size_type count, const T& value );
   template< class InputIt >
   iterator insert( const_iterator pos, InputIt first, InputIt last   \fB(4)\fP
   );
   iterator insert( const_iterator pos, std::initializer_list<T>      \fB(5)\fP \fI(since C++11)\fP
   ilist );

   Inserts elements at the specified location in the container.

   1,2) Inserts value before pos.
   3) Inserts count copies of the value before pos.
   4) Inserts elements from range [first, last) before pos.

   This overload has the same effect as overload \fB(3)\fP if InputIt is an     \fI(until C++11)\fP
   integral type.
   This overload participates in overload resolution only if InputIt
   qualifies as LegacyInputIterator, to avoid ambiguity with the overload \fI(since C++11)\fP
   \fB(3)\fP.

   If first and last are iterators into *this, the behavior is undefined.
   5) Inserts elements from initializer list ilist before pos.

   All iterators (including the end() iterator) are invalidated. References are
   invalidated too, unless pos == begin() or pos == end(), in which case they are not
   invalidated.

.SH Parameters

   pos                 -         iterator before which the content will be inserted
                                 (pos may be the end() iterator)
   value               -         element value to insert
   count               -         number of elements to insert
   first, last         -         the range of elements to insert, cannot be iterators
                                 into container for which insert is called
   ilist               -         initializer list to insert the values from
.SH Type requirements
   -
   T must meet the requirements of CopyAssignable and CopyInsertable in order to use
   overload \fB(1)\fP.
   -
   T must meet the requirements of MoveAssignable and MoveInsertable in order to use
   overload \fB(2)\fP.
   -
   T must meet the requirements of CopyAssignable and CopyInsertable in order to use
   overload \fB(3)\fP.
   -
   T must meet the requirements of EmplaceConstructible in order to use overloads
   (4,5).
   -
   T must meet the requirements of Swappable, MoveAssignable, MoveConstructible and
   MoveInsertable in order to use overloads (4,5). \fI(since C++17)\fP

.SH Return value

   1,2) Iterator pointing to the inserted value.
   3) Iterator pointing to the first element inserted, or pos if count == 0.
   4) Iterator pointing to the first element inserted, or pos if first == last.
   5) Iterator pointing to the first element inserted, or pos if ilist is empty.

.SH Complexity

   1,2) Constant plus linear in the lesser of the distances between pos and either of
   the ends of the container.
   3) Linear in count plus linear in the lesser of the distances between pos and either
   of the ends of the container.
   4) Linear in std::distance(first, last) plus linear in the lesser of the distances
   between pos and either of the ends of the container.
   5) Linear in ilist.size() plus linear in the lesser of the distances between pos and
   either of the ends of the container.

.SH Exceptions

   If an exception is thrown other than by

     * the copy constructor of T,

     * the move constructor of T,   \fI(since C++11)\fP

     * the copy assignment operator of T,

     * the move assignment operator of T,   \fI(since C++11)\fP

   this function has no effect \fI(strong exception guarantee)\fP.

   If an exception is thrown when inserting a single element at either
   end, this function has no effect \fI(strong exception guarantee)\fP.         \fI(since C++11)\fP
   Otherwise, if an exception is thrown by the move constructor of a
   non-CopyInsertable T, the effects are unspecified.

.SH Example


// Run this code

 #include <iostream>
 #include <iterator>
 #include <deque>

 void print(int id, const std::deque<int>& container)
 {
     std::cout << id << ". ";
     for (const int x : container)
         std::cout << x << ' ';
     std::cout << '\\n';
 }

 int main ()
 {
     std::deque<int> c1(3, 100);
     print(1, c1);

     auto it = c1.begin();
     it = c1.insert(it, 200);
     print(2, c1);

     c1.insert(it, 2, 300);
     print(3, c1);

     // reset `it` to the begin:
     it = c1.begin();

     std::deque<int> c2(2, 400);
     c1.insert(std::next(it, 2), c2.begin(), c2.end());
     print(4, c1);

     int arr[] = {501, 502, 503};
     c1.insert(c1.begin(), arr, arr + std::size(arr));
     print(5, c1);

     c1.insert(c1.end(), {601, 602, 603});
     print(6, c1);
 }

.SH Output:

 1. 100 100 100
 2. 200 100 100 100
 3. 300 300 200 100 100 100
 4. 300 300 400 400 200 100 100 100
 5. 501 502 503 300 300 400 400 200 100 100 100
 6. 501 502 503 300 300 400 400 200 100 100 100 601 602 603

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to        Behavior as published               Correct behavior
   LWG 149 C++98      overloads \fB(3)\fP and \fB(4)\fP returned       returns an iterator
                      nothing
   LWG 247 C++98      the complexity was only specified    also specified for inserting
                      for inserting a single element       multiple elements

.SH See also

   emplace    constructs element in-place
   \fI(C++11)\fP    \fI(public member function)\fP
   push_front inserts an element to the beginning
              \fI(public member function)\fP
   push_back  adds an element to the end
              \fI(public member function)\fP
   inserter   creates a std::insert_iterator of type inferred from the argument
              \fI(function template)\fP
